package cn.org.xiaosheng.controller;

import cn.org.xiaosheng.bean.Logs;
import cn.org.xiaosheng.bean.User;
import cn.org.xiaosheng.service.AnalysisLogsService;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author XiaoSheng
 * @date 2024/9/10 上午2:27
 */
@RestController
@RequestMapping("/logs")
public class LogsController {

    @Resource
    private AnalysisLogsService analysisLogsService;

    @GetMapping("/renew")
    @CrossOrigin(origins = "*")
    public boolean analysisLogs() {
        return analysisLogsService.renewLogs();
    }

    // 初步认为webflux只能推送流类型数据,而不能推送json类型数据
    @GetMapping(value = "/flux", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    @CrossOrigin(origins = "*")
    public Flux<Logs> logsDisplay() {
        // 需要注意返回的应该是流式的数据,而不是静态的对象
        return analysisLogsService.logsDisplay().onErrorResume(e -> Flux.empty());
    }

    // 只展示有效数据
    @GetMapping(value = "/fluxValid", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    @CrossOrigin(origins = "*")
    public Flux<Logs> logsDisplayValid() {
        // 需要注意返回的应该是流式的数据,而不是静态的对象
        return analysisLogsService.logsDisplayValid().onErrorResume(e -> Flux.empty());
    }

    @GetMapping("/getIp")
    @CrossOrigin(origins = "*")
    public List<Logs> getIp(@RequestParam("ip") String ip) {
        return analysisLogsService.getIpData(ip);
    }

    // 查询ip访问次数超过6次且访问代码不为200
    @GetMapping("/fluxIp")
    @CrossOrigin(origins = "*")
    public List<String> logsDisplayIp() {
        return analysisLogsService.logsDisplayIp();
    }

    @GetMapping("/delLogs")
    public void delLogs() {
        analysisLogsService.delLogs();
    }
    
}
